package roborally;

import Exceptions.TerminationError;
import be.kuleuven.cs.som.annotate.Raw;

/**
 * A class representing repairkits with a weight and energy to restore a robot's maximum energy.
 * @author Jef Keijers/Bachelor of Science: Informatics
 * @version 1.0
 */
public class RepairKit extends Item {	  
	/**
	 * @param 	energy
	 * @param 	weight
	 * @pre
	 * 			|canHaveAsEnergy(energy)
	 * @effect	
	 * 			|super(weight);
	 * @effect	
	 * 			|this.setEnergy(new Energy(initialEnergy()))
	 */
	public RepairKit(double initialEnergy, int weight) {
		super(weight);
		assert(this.canHaveAsEnergy(initialEnergy));
		this.setEnergy(new Energy(initialEnergy));
	}	

	/**		
	 * @param energy
	 * @return	
	 * 		| if ((energy <= Double.MAX_VALUE)&&(energy>=0)) then result == true else result == false
	 */
	private boolean canHaveAsEnergy(double energy) {
		if ((energy<=Double.MAX_VALUE) && (energy >=0)) 
			return true;
		return false;
	}

	/**
	 * @post 
	 * 			| new.energy.getEnergy() == this.getEnergy().getValue()+ getEnergyToAddWhenShot()
	 */
	@Override
	public void handleShot() {
		this.getEnergy().setValue(this.getEnergy().getValue()+getEnergyToAddWhenShot());
	}


	/**
	 * @throws 	TerminationError 
	 * 			|this.terminate()
	 */
	@Override @Raw
	public void handleUse(Robot robot) throws TerminationError  {
		double amountOfEnergyThatRepairKitHasToHaveToFillMaxEnergy=(20000-robot.getMaxEnergy())*2;	
		if (amountOfEnergyThatRepairKitHasToHaveToFillMaxEnergy<this.getEnergy().getValue()) {
			robot.setMaxEnergy(20000);
			this.getEnergy().setValue(this.getEnergy().getValue()-amountOfEnergyThatRepairKitHasToHaveToFillMaxEnergy);
		} else if (amountOfEnergyThatRepairKitHasToHaveToFillMaxEnergy>=this.getEnergy().getValue()) {
			robot.setMaxEnergy(robot.getMaxEnergy()+(this.getEnergy().getValue()/2));
			robot.terminateFromPossessions(this);
			this.terminate();
		}
	}

}
